
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
	<meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Test XSeq</title>
    <script src="xlib.js"></script>
	<style>
	</style>
  </head>  
  <body>
    <div id="result"></div>
    <script>
let error_count = 0, tc_count = 0, log = ""

function assert(criteria, fail_msg, succ_msg=""){
  tc_count++
  
  if( criteria ){
    log += `<p>${succ_msg}</p>`
  }else{
    error_count++
    log += `<p>${fail_msg}</p>`
    console.error( fail_msg )
  }
}

// ======== 简单类型匹配
{
  const vs = [[1,1], [2,1], [3,1], [4,1]]
  const res = XLIB.Math.wavg( vs, (x)=>x[0], (x)=>x[1] )
  assert( res===2.5, `加权平均计算错误: ${res}!=2.5` )
}


// ========  Permutation
{
  let patt = ["X", "Y", "Z"]
  let res = XLIB.Math.permutation(patt)
  let expert = [
    ['X','Y','Z'],
    ['X','Z','Y'],
    ['Y','X','Z'],
    ['Y','Z','X'],
    ['Z','X','Y'],
    ['Z','Y','X'],
  ]
  assert( res.toString() === expert.toString(), `3元素排列测试失败: ${res}!=${expert}` )
}

// ========  Recycle
{
  let patt = ['X', 'Y', 'Z']
  let res = XLIB.Math.recycle(patt)
  let expert = [
    ['X','Y','Z'],
    ['Y','Z','X'],
    ['Z','X','Y'],
  ]
  for( let i=0; i<3; i++ ){
    assert( res[i].toString() === expert[i].toString(), `3元素循环遍历失败: ${res[i]}!=${expert[i]}` )
  }
}

//=====================================================================================================
if( error_count==0 ) console.log(`%cSuccess: All Test Case Pass!`, 'color:green')

const rst = document.getElementById("result")
rst.appendChild(document.createTextNode(`成功率: ${tc_count-error_count}/${tc_count}`))
const logE = document.createElement("div")
logE.innerHTML = log
rst.appendChild(logE)

    </script>
  </body>
</html>
    
